<#assign className = table.className>
<#assign classNameLower = className?uncap_first>
<#assign classNameAllLower = className?lower_case>
<#assign hasParentSearchField = 'false'>
package ${basePackage}.${moduleName}.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.web.annotation.RequestObjectFieldIgnore;
<#if adminHasAdd == 'true' || adminHasEdit == 'true' || adminHasDelete == 'true' || adminHasAudit == 'true'>
import ${basePackage}.common.utils.JsonResult;
</#if>
import ${basePackage}.common.utils.PageResult;
import ${basePackage}.${moduleName}.model.entity.${className};
import ${basePackage}.${moduleName}.model.request.${className}Form;
import ${basePackage}.${moduleName}.service.${className}Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
<#if adminSearchField?? && adminSearchField != "">
    <#list adminSearchField?split(",") as field>
        <#if field?contains("_") && field?contains(".")>
            <#assign hasParentSearchField = 'true'>
import java.util.HashMap;
import java.util.Map;
            <#break>
        </#if>
    </#list>
</#if>
import cn.hutool.core.util.StrUtil;
<#if hasParentSearchField = 'false'>
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
</#if>
<#if adminSearchField?? && adminSearchField != "">
    <#list adminSearchField?split(",") as field>
        <#list table.columns as column>
            <#if column.sqlName == field && column.isDateTimeColumn>
import org.springframework.format.annotation.DateTimeFormat;
import java.time.LocalDateTime;
                <#break>
            </#if>
        </#list>
    </#list>
</#if>
<#if adminSearchField?? && adminSearchField != "">
    <#list adminSearchField?split(",") as field>
        <#if field?contains("_") && field?contains(".")>
            <#if parentTableList?? && (parentTableList?size>0)>
                <#list parentTableList as parentTable>
                    <#if adminSearchField?? && adminSearchField != "">
                        <#if field?contains(parentTable.sqlName)>
                            <#list parentTable.columns as parentColumn>
                                <#if parentColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                    <#if parentColumn.isEnum>
import ${basePackage}.${moduleName}.model.entity.${parentTable.className};
                                    </#if>
                                    <#break>
                                </#if>
                            </#list>
                        </#if>
                    </#if>
                </#list>
            </#if>
        </#if>
    </#list>
</#if>

/**
 *
 * @author
 */
@Validated
@Api(value = "Admin-${table.remarks}管理", tags = "#/${moduleName}/${classNameLower}")
@RestController("${classNameLower}Controller")
@RequestMapping("${moduleName}/admin/${classNameLower}")
@RequiredArgsConstructor
public class ${className}Controller {

    private final ${className}Service ${classNameLower}Service;


    @ApiOperation(value = "查询所有${table.remarks}", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "第几页", defaultValue = "1", example = "1", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "limit", value = "每页多少条", defaultValue = "10", example = "10", dataType = "Integer", paramType = "query")
            ,@ApiImplicitParam(name = "sort", value="排序字段名", defaultValue = "create_time", example = "create_time", dataType = "String", paramType = "query")
            ,@ApiImplicitParam(name = "order", value="排序规则(asc:正序, desc:倒序)", defaultValue = "asc", example = "asc", dataType = "String", paramType = "query")
        <#if adminSearchField?? && adminSearchField != "">
            <#list adminSearchField?split(",") as field>
                <#if field?contains("_") && field?contains(".")>
                    <#if ancestorTableList?? && (ancestorTableList?size>0)>
                        <#list ancestorTableList as ancestorTable>
                            <#if adminSearchField?? && adminSearchField != "">
                                <#if field?contains(ancestorTable.sqlName)>
                                    <#list ancestorTable.columns as ancestorColumn>
                                        <#if ancestorColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                            <#if ancestorColumn.isEnum>
            ,@ApiImplicitParam(name = "${ancestorTable.className?uncap_first}${ancestorColumn.columnName}", value = "${ancestorTable.className?uncap_first}${ancestorColumn.columnAlias}", defaultValue = "", example = "", dataType = "${ancestorColumn.simpleJavaType}", paramType = "query")
                                            <#else>
            ,@ApiImplicitParam(name = "${ancestorTable.className?uncap_first}${ancestorColumn.columnName}", value = "${ancestorTable.className?uncap_first}${ancestorColumn.columnAlias}", defaultValue = "", example = "", dataType = "${ancestorColumn.simpleJavaType}", paramType = "query")
                                            </#if>
                                            <#break>
                                        </#if>
                                    </#list>
                                </#if>
                            </#if>
                        </#list>
                    </#if>
                    <#if parentTableList?? && (parentTableList?size>0)>
                        <#list parentTableList as parentTable>
                            <#if adminSearchField?? && adminSearchField != "">
                                <#if field?contains(parentTable.sqlName)>
                                    <#list parentTable.columns as parentColumn>
                                        <#if parentColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                            <#if parentColumn.isEnum>
            ,@ApiImplicitParam(name = "${parentTable.className?uncap_first}${parentColumn.columnName}", value="${parentTable.className?uncap_first}${parentColumn.columnAlias}", defaultValue = "", example = "", dataType = "${parentColumn.simpleJavaType}", paramType = "query")
                                            <#else>
            ,@ApiImplicitParam(name = "${parentTable.className?uncap_first}${parentColumn.columnName}", value="${parentTable.className?uncap_first}${parentColumn.columnAlias}", defaultValue = "", example = "", dataType = "${parentColumn.simpleJavaType}", paramType = "query")
                                            </#if>
                                            <#break>
                                        </#if>
                                    </#list>
                                </#if>
                            </#if>
                        </#list>
                    </#if>
                <#else>
                    <#list table.columns as column>
                        <#if column.sqlName == field>
                            <#if column.isEnum>
            ,@ApiImplicitParam(name = "${column.columnNameLower}", value = "${column.columnAlias}", defaultValue = "", example = "", dataType = "${column.simpleJavaType}", paramType = "query")
                            <#elseif column.isDateTimeColumn>
            ,@ApiImplicitParam(name = "start${column.columnNameLower}", value = "开始${column.columnAlias}", defaultValue = "", example = "", dataType = "${column.simpleJavaType}", paramType = "query")
            ,@ApiImplicitParam(name = "end${column.columnNameLower}", value = "结束${column.columnAlias}", defaultValue = "", example = "", dataType = "${column.simpleJavaType}", paramType = "query")
                            <#else>
            ,@ApiImplicitParam(name = "${column.columnNameLower}", value = "${column.columnAlias}", defaultValue = "", example = "", dataType = "${column.simpleJavaType}", paramType = "query")
                            </#if>
                            <#break>
                        </#if>
                    </#list>
                </#if>
            </#list>
        </#if>
    })
    @GetMapping("/list")
    public PageResult<${className}> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "asc") String order
        <#if adminSearchField?? && adminSearchField != "">
            <#list adminSearchField?split(",") as field>
                <#if field?contains("_") && field?contains(".")>
                    <#if ancestorTableList?? && (ancestorTableList?size>0)>
                        <#list ancestorTableList as ancestorTable>
                            <#if adminSearchField?? && adminSearchField != "">
                                <#if field?contains(ancestorTable.sqlName)>
                                    <#list ancestorTable.columns as ancestorColumn>
                                        <#if ancestorColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                            <#if ancestorColumn.isEnum>
            ,${ancestorTable.className?uncap_first}${ancestorColumn.columnName}
                                            <#else>
            ,${ancestorColumn.simpleJavaType} ${ancestorTable.className?uncap_first}${ancestorColumn.columnName}
                                            </#if>
                                            <#break>
                                        </#if>
                                    </#list>
                                </#if>
                            </#if>
                        </#list>
                    </#if>
                    <#if parentTableList?? && (parentTableList?size>0)>
                        <#list parentTableList as parentTable>
                            <#if adminSearchField?? && adminSearchField != "">
                                <#if field?contains(parentTable.sqlName)>
                                    <#list parentTable.columns as parentColumn>
                                        <#if parentColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                            <#if parentColumn.isEnum>
            ,${parentTable.className}.${parentColumn.columnName} ${parentTable.className?uncap_first}${parentColumn.columnName}
                                            <#else>
            ,${parentColumn.simpleJavaType} ${parentTable.className?uncap_first}${parentColumn.columnName}
                                            </#if>
                                            <#break>
                                        </#if>
                                    </#list>
                                </#if>
                            </#if>
                        </#list>
                    </#if>
                <#else>
                    <#list table.columns as column>
                        <#if column.sqlName == field>
                            <#if column.isEnum>
            ,${className}.${column.columnName} ${column.columnNameLower}
                            <#elseif column.isDateTimeColumn>
            ,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") ${column.simpleJavaType} start${column.columnName}
            ,@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") ${column.simpleJavaType} end${column.columnName}
                            <#else>
            ,${column.simpleJavaType} ${column.columnNameLower}
                            </#if>
                            <#break>
                        </#if>
                    </#list>
                </#if>
            </#list>
        </#if>
    ){

        <#if hasParentSearchField = 'true'>
        Page<${className}> pg = new Page<>(page, limit);
        Map<String, Object> params = new HashMap<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            params.put("sort", StrUtil.toUnderlineCase(sort));
            params.put("order", StrUtil.toUnderlineCase(order));
        }
            <#if adminSearchField?? && adminSearchField != "">

        //搜索
                <#list adminSearchField?split(",") as field>
                    <#if field?contains("_") && field?contains(".")>
                        <#if ancestorTableList?? && (ancestorTableList?size>0)>
                            <#list ancestorTableList as ancestorTable>
                                <#if adminSearchField?? && adminSearchField != "">
                                    <#if field?contains(ancestorTable.sqlName)>
                                        <#list ancestorTable.columns as ancestorColumn>
                                            <#if ancestorColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                                <#if ancestorColumn.isEnum>
        if (${ancestorTable.className?uncap_first}${ancestorColumn.columnName} !=null){
                                                    params.put("${ancestorTable.className?uncap_first}${ancestorColumn.columnName}" , ${ancestorTable.className?uncap_first}${ancestorColumn.columnName}.getValue().toString());
                                                }
                                                <#elseif ancestorColumn.simpleJavaType == "String">
        if (StringUtils.isNotBlank(${ancestorTable.className?uncap_first}${ancestorColumn.columnName})) {
            params.put("${ancestorTable.className?uncap_first}${ancestorColumn.columnName}", ${ancestorTable.className?uncap_first}${ancestorColumn.columnName});
        }
                                                <#else>
        if (${ancestorTable.className?uncap_first}${ancestorColumn.columnName} !=null){
            params.put("${ancestorTable.className?uncap_first}${ancestorColumn.columnName}", ${ancestorTable.className?uncap_first}${ancestorColumn.columnName});
        }
                                                </#if>
                                                <#break>
                                            </#if>
                                        </#list>
                                    </#if>
                                </#if>
                            </#list>
                        </#if>
                        <#if parentTableList?? && (parentTableList?size>0)>
                            <#list parentTableList as parentTable>
                                <#if adminSearchField?? && adminSearchField != "">
                                    <#if field?contains(parentTable.sqlName)>
                                        <#list parentTable.columns as parentColumn>
                                            <#if parentColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                                <#if parentColumn.isEnum>
        if (${parentTable.className?uncap_first}${parentColumn.columnName} !=null){
            params.put("${parentTable.className?uncap_first}${parentColumn.columnName}", ${parentTable.className?uncap_first}${parentColumn.columnName}.getValue().toString());
        }
                                                <#elseif parentColumn.simpleJavaType == "String">
        if (StringUtils.isNotBlank(${parentTable.className?uncap_first}${parentColumn.columnName})) {
            params.put("${parentTable.className?uncap_first}${parentColumn.columnName}", ${parentTable.className?uncap_first}${parentColumn.columnName});
        }
                                                <#else>
        if (${parentTable.className?uncap_first}${parentColumn.columnName} !=null){
            params.put("${parentTable.className?uncap_first}${parentColumn.columnName}" , ${parentTable.className?uncap_first}${parentColumn.columnName});
        }
                                                </#if>
                                                <#break>
                                            </#if>
                                        </#list>
                                    </#if>
                                </#if>
                            </#list>
                        </#if>
                    <#else>
                        <#list table.columns as column>
                            <#if column.sqlName == field>
                                <#if column.isDateTimeColumn>
        if (start${column.columnName} != null) {
            params.put("start${column.columnName}", start${column.columnName});
        }
        if (end${column.columnName} != null) {
            params.put("end${column.columnName}", end${column.columnName});
        }
                                <#elseif column.isEnum>
        if (${column.columnNameLower} !=null){
            params.put("${column.sqlName}" , ${column.columnNameLower}.getValue().toString());
        }
                                <#elseif column.simpleJavaType == "String">
        if (StringUtils.isNotBlank(${column.columnNameLower})) {
            params.put("${column.sqlName}", ${column.columnNameLower});
        }
                                <#else>
        if (${column.columnNameLower} !=null){
            params.put("${column.sqlName}" , ${column.columnNameLower});
        }
                                </#if>
                                <#break>
                            </#if>
                        </#list>
                    </#if>
                </#list>
            </#if>

        IPage<${className}> list = ${classNameLower}Service.pageList(pg, params);
        return new PageResult<>(list.getRecords(), list.getTotal());
        <#else>
        Page<${className}> pg = new Page<>(page, limit);
        QueryWrapper<${className}> queryWrapper = new QueryWrapper<>();

        //排序
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            if (StringUtils.equals("asc", order)) {
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(sort));
            } else {
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(sort));
            }
        }
            <#if adminSearchField?? && adminSearchField != "">

        //搜索
                <#list adminSearchField?split(",") as field>
                    <#if field?contains("_") && field?contains(".")>
                        <#if ancestorTableList?? && (ancestorTableList?size>0)>
                            <#list ancestorTableList as ancestorTable>
                                <#if adminSearchField?? && adminSearchField != "">
                                    <#if field?contains(ancestorTable.sqlName)>
                                        <#list ancestorTable.columns as ancestorColumn>
                                            <#if ancestorColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                                <#if ancestorColumn.isEnum>
        if (${ancestorTable.className?uncap_first}${ancestorColumn.columnName} !=null){
            queryWrapper.eq("${ancestorColumn.sqlName}", ${ancestorTable.className?uncap_first}${ancestorColumn.columnName}.getValue());
        }
                                                <#elseif ancestorColumn.simpleJavaType == "String">
        if (StringUtils.isNotBlank(${ancestorTable.className?uncap_first}${ancestorColumn.columnName})) {
            queryWrapper.like("${ancestorTable.className?uncap_first}${ancestorColumn.columnName}" , ${ancestorTable.className?uncap_first}${ancestorColumn.columnName});
        }
                                                <#else>
        if (${ancestorTable.className?uncap_first}${ancestorColumn.columnName} !=null){
            queryWrapper.eq("${ancestorTable.className?uncap_first}${ancestorColumn.columnName}" , ${ancestorTable.className?uncap_first}${ancestorColumn.columnName});
        }
                                                </#if>
                                                <#break>
                                            </#if>
                                        </#list>
                                    </#if>
                                </#if>
                            </#list>
                        </#if>
                        <#if parentTableList?? && (parentTableList?size>0)>
                            <#list parentTableList as parentTable>
                                <#if adminSearchField?? && adminSearchField != "">
                                    <#if field?contains(parentTable.sqlName)>
                                        <#list parentTable.columns as parentColumn>
                                            <#if parentColumn.sqlName == field?substring(field?index_of(".")+1, field?length)>
                                                <#if parentColumn.isEnum>
        if (${parentTable.className?uncap_first}${parentColumn.columnName} !=null){
            queryWrapper.eq("${parentColumn.sqlName}" , ${parentTable.className?uncap_first}${parentColumn.columnName}.getValue());
        }
                                                <#elseif parentColumn.simpleJavaType == "String">
        if (StringUtils.isNotBlank(${parentTable.className?uncap_first}${parentColumn.columnName})) {
            queryWrapper.like("${parentTable.className?uncap_first}${parentColumn.columnName}", ${parentTable.className?uncap_first}${parentColumn.columnName});
        }
                                                <#else>
        if (${parentTable.className?uncap_first}${parentColumn.columnName} !=null){
            queryWrapper.eq("${parentTable.className?uncap_first}${parentColumn.columnName}", ${parentTable.className?uncap_first}${parentColumn.columnName});
        }
                                                </#if>
                                                <#break>
                                            </#if>
                                        </#list>
                                    </#if>
                                </#if>
                            </#list>
                        </#if>
                    <#else>
                        <#list table.columns as column>
                            <#if column.sqlName == field>
                                <#if column.isDateTimeColumn>
        if (start${column.columnName} != null) {
            queryWrapper.ge("${column.sqlName}", start${column.columnName});
        }
        if (end${column.columnName} != null) {
            queryWrapper.le("${column.sqlName}", end${column.columnName});
        }
                                <#elseif column.isEnum>
        if (${column.columnNameLower} !=null){
            queryWrapper.eq("${column.sqlName}", ${column.columnNameLower}.getValue());
        }
                                <#elseif column.simpleJavaType == "String">
        if (StringUtils.isNotBlank(${column.columnNameLower})) {
            queryWrapper.like("${column.sqlName}", ${column.columnNameLower});
        }
                                <#else>
        if (${column.columnNameLower} !=null){
            queryWrapper.eq("${column.sqlName}", ${column.columnNameLower});
        }
                                </#if>
                                <#break>
                            </#if>
                        </#list>
                    </#if>
                </#list>
            </#if>

        IPage<${className}> list = ${classNameLower}Service.getBaseMapper().selectPage(pg, queryWrapper);
        return new PageResult<>(list.getRecords(), list.getTotal());
        </#if>
    }
    <#if adminHasAdd == 'true'>



    @ApiOperation(value = "添加${table.remarks}", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(${className}Form.Add.class) ${className}Form ${classNameLower}Form) {
        if (${classNameLower}Service.add(${classNameLower}Form) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    </#if>
    <#if adminHasEdit == 'true'>



    @ApiOperation(value = "修改${table.remarks}", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(${className}Form.Update.class) ${className}Form ${classNameLower}Form) {
        ${className} ${classNameLower} = ${classNameLower}Service.getById(${classNameLower}Form.getId());
        if(${classNameLower} == null){
        return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(${className}.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(${classNameLower}Form, ${classNameLower}, copyOptions);
        if (${classNameLower}Service.updateById(${classNameLower})) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    </#if>
    <#if adminHasDelete == 'true'>


    @ApiOperation(value = "删除${table.remarks}", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "${classNameLower}Id", value = "${table.remarks}id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long ${classNameLower}Id) {
        if (${classNameLower}Service.removeById(${classNameLower}Id)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
    </#if>
}
